package org.brisling.common.util;

import java.util.LinkedHashMap;
import java.util.Map;

import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;

public class WhereUtil {

	private static WhereUtil util;
	
	private WhereUtil(){}
	
	public static WhereUtil getInstance(){
		if(util == null)
			util = new WhereUtil();
		return util;
	}
	
	@SuppressWarnings("unchecked")
	public static String getWhereCorse(Map<String,Object> conditionMap,Map<String,String> typeMap){
		getInstance();
		
		String whereCorse = null;		
		String fuzzyWhere = null;
		whereCorse = getConditionWhereCorse((Map<String, String>) conditionMap.get("condition"),typeMap);
		
		if(conditionMap.containsKey("fuzzy")){
			fuzzyWhere = getFuzzyWhereCorse((Map<String, String>) conditionMap.get("fuzzy"),typeMap);
		}
		
		
		if(whereCorse==null || whereCorse.trim().length()==0){
			if(fuzzyWhere!=null && fuzzyWhere.trim().length()>0){
				whereCorse = fuzzyWhere.trim();
			}
		}else{
			if(fuzzyWhere!=null && fuzzyWhere.trim().length()>0){
				whereCorse += " and (" +fuzzyWhere + ")";
			}
		}
		
		return whereCorse;	
	}
	
	/**
	 * 获取高级查询条件
	 * @param map	字段、值map
	 * @param typeMap	字段、类型map
	 * @return
	 */
	public static String getConditionWhereCorse(Map<String,String> map,Map<String,String> typeMap){		
		getInstance();
		String retStr = getTypeWhereCorse(map,typeMap,null);
		return retStr==null?"":retStr;
	}
	

	/**
	 * 获取高级查询条件
	 * @param map	字段、值map
	 * @param typeMap	字段、类型map
	 * @return
	 */
	protected String getLikeWhereCorse(Map<String,String> map,Map<String,String> typeMap){
		getInstance();
		
		String retStr =getTypeWhereCorse(map,typeMap," LIKE ");
		
		return retStr;
	}
	
	/**
	 * 获取模糊查询条件
	 * @param map	字段、值map
	 * @param typeMap	字段、类型map
	 * @return
	 */
	public static String getFuzzyWhereCorse(Map<String,String> map,Map<String,String> typeMap){		
		getInstance();
		String retStr = getTypeWhereCorse(map,typeMap," OR ");
		return retStr==null?"":retStr;
	}
	
	/**
	 * 获取高级查询条件
	 * @param map	字段、值map
	 * @param typeMap	字段、类型map
	 * @return
	 */
	public static String getTypeWhereCorse(Map<String,String> map,Map<String,String> typeMap,String conditionType){
		getInstance();
		if((map==null) || map.isEmpty()){
			return null;
		}
		String _conditionType = " AND ";
		_conditionType = conditionType==null?_conditionType:conditionType;
		String retStr =null;
		if(typeMap!=null&&!typeMap.isEmpty()){
			
			for(Map.Entry<String, String> entry:typeMap.entrySet()){
				
				String _key = entry.getKey();
				String _type = entry.getValue();
				if(_type!=null){
					_type=_type.trim();
					if(_type.length()>0){
						String _tmpStr = null;
						switch(_type){
							case ConstSysParam.INTEGER	
								:
									String intTmp = null;
									if(map.containsKey(_key))
										_tmpStr = String.format(SysConfig.getConfig("numberTemplate"),_key ,map.get(_key));
									if(map.containsKey(_key+"_le")){
										_tmpStr = String.format(SysConfig.getConfig("numberLeTemplate"),_key ,map.get(_key+"_le"));
									}
									
									if(map.containsKey(_key+"_less")){
										_tmpStr = String.format(SysConfig.getConfig("numberLessTemplate"),_key ,map.get(_key+"_less"));
									}
									
									if(map.containsKey(_key+"_ge")){
										intTmp = String.format(SysConfig.getConfig("numberGeTemplate"),_key ,map.get(_key+"_ge"));
										_tmpStr = _tmpStr==null?intTmp:(_tmpStr +=_conditionType+intTmp);
									}
									
									if(map.containsKey(_key+"_great")){
										intTmp = String.format(SysConfig.getConfig("numberGreatTemplate"),_key ,map.get(_key+"_great"));
										_tmpStr = _tmpStr==null?intTmp:(_tmpStr +=_conditionType+intTmp);
									}
									if(map.containsKey(_key + "_in"))
										_tmpStr = String.format(SysConfig.getConfig("numberInTemplate"),_key ,map.get(_key + "_in"));
									
									if(map.containsKey(_key + "_ne"))
										_tmpStr = String.format(SysConfig.getConfig("numberNeTemplate"),_key ,map.get(_key + "_ne"));
								break;
							case ConstSysParam.STRING
								:
									if(map.containsKey(_key))
										_tmpStr = String.format(SysConfig.getConfig("strTemplate"),_key ,map.get(_key));
									
									if(map.containsKey(_key + "_in")){
										String _val = map.get(_key + "_in");
										if(_val!=null){
											_val = _val.trim();
											String[] varArray = _val.split(",");
											String tmpVal = null;
											if(varArray!=null && varArray.length>0){
												for(int i = 0;i<varArray.length;i++){
													tmpVal = tmpVal==null?"'"+varArray[i]+"'":tmpVal+",'"+varArray[i]+"'";
												}
												_tmpStr = String.format(SysConfig.getConfig("strInTemplate"),_key ,tmpVal);
											}
										}
									}
										
									
									if(map.containsKey(_key + "_like"))
										_tmpStr = String.format(SysConfig.getConfig("strLikeTemplate"),_key ,map.get(_key + "_like"));
									if(map.containsKey(_key + "_le"))
										_tmpStr = String.format(SysConfig.getConfig("strLeTemplate"),_key ,map.get(_key + "_le"));
									
									if(map.containsKey(_key + "_ge"))
										_tmpStr = String.format(SysConfig.getConfig("strGeTemplate"),_key ,map.get(_key + "_ge"));
									
									if(map.containsKey(_key + "_great"))
										_tmpStr = String.format(SysConfig.getConfig("strGreatTemplate"),_key ,map.get(_key + "_great"));
									
									if(map.containsKey(_key + "_ne"))
										_tmpStr = String.format(SysConfig.getConfig("strNeTemplate"),_key ,map.get(_key + "_ne"));
								break;
							case ConstSysParam.DATE
							:
								String _timeTmp = null;
								if(map.containsKey(_key))
									_tmpStr = String.format(SysConfig.getConfig("dateTemplate"),_key ,map.get(_key));
								if(map.containsKey(_key+"_start")){
									_timeTmp = String.format(SysConfig.getConfig("dateGeTemplate"),_key,map.get(_key+"_start"));
									_tmpStr = _tmpStr==null?_timeTmp:(_tmpStr +=_conditionType+_timeTmp);
								}
								if(map.containsKey(_key+"_end")){
									_timeTmp = String.format(SysConfig.getConfig("dateLeTemplate"),_key ,map.get(_key+"_end"));
									_tmpStr = _tmpStr==null?_timeTmp:(_tmpStr +=_conditionType+_timeTmp);
								}
								
								if(map.containsKey(_key+"_ge_or_null")){
									_timeTmp = String.format(SysConfig.getConfig("DateGeOrNullTemplate"),_key ,map.get(_key+"_ge_or_null"));
									_tmpStr = _tmpStr==null?_timeTmp:(_tmpStr +=_conditionType+_timeTmp);
								}
								break;
													
							default:
								_tmpStr=null;							
						}
						
						if(_tmpStr!=null)
							retStr=retStr==null?_tmpStr:(retStr += _conditionType +_tmpStr);
					}
				}
			}
			
		}
		return retStr;
	}
	
	/**
	 * 获取动态查询排序条件
	 * @param sort
	 * @return
	 */
	public static String getOrderCorse(LinkedHashMap<String,String> sort){
		getInstance();
		String retStr = null;
		if((sort!=null)&&(!sort.isEmpty())){
			
			String _tmpStr = null;
			for(Map.Entry<String, String> entry:sort.entrySet()){
				String sortKey = entry.getKey();
				String sortValue = entry.getValue();
				if(sortKey!=null){
					if((sortValue==null)||(sortValue.trim().length()<=0)||(sortValue.toUpperCase().compareTo("ASC")==0)){				
						_tmpStr=sortKey + " ASC ";	
					}else{
						_tmpStr=sortKey + " DESC ";
					}
				}
				
				if(_tmpStr!=null){				
					retStr=retStr==null?_tmpStr:(retStr + "," +_tmpStr);
				}
			}			
		}
		
		return retStr;
	}
}
